package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
  private final ShoppingCartMapper shoppingCartMapper;
  private final DishMapper dishMapper;
  private final SetmealMapper setmealMapper;
  
  public ShoppingCartServiceImpl(ShoppingCartMapper shoppingCartMapper, DishMapper dishMapper, SetmealMapper setmealMapper) {
    this.shoppingCartMapper = shoppingCartMapper;
    this.dishMapper = dishMapper;
    this.setmealMapper = setmealMapper;
  }
  
  /**
   * 添加购物车
   * @param shoppingCartDTO
   */
  @Override
  public void add(ShoppingCartDTO shoppingCartDTO) {
    // 1.判断当前加入购物车的商品是否已经存在
    ShoppingCart shoppingCart = new ShoppingCart();
    BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
    Long userId = BaseContext.getCurrentId();
    shoppingCart.setUserId(userId);
    List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
    
    // 2.如果已经存在，只需要将数量+1
    if(list != null && !list.isEmpty()) {
      ShoppingCart cart = list.get(0);
      cart.setNumber(cart.getNumber() + 1);
      shoppingCartMapper.update(cart);
    }else {
      // 3.如果不存在，插入新数据
      if(shoppingCartDTO.getDishId() != null) {
        // 添加的是菜品
        DishVO dishVO = dishMapper.findById(shoppingCartDTO.getDishId());
        shoppingCart.setName(dishVO.getName());
        shoppingCart.setImage(dishVO.getImage());
        shoppingCart.setAmount(dishVO.getPrice());
      }else {
        // 添加的是套餐
        SetmealVO setmealVO = setmealMapper.findById(shoppingCartDTO.getSetmealId());
        shoppingCart.setName(setmealVO.getName());
        shoppingCart.setImage(setmealVO.getImage());
        shoppingCart.setAmount(setmealVO.getPrice());
      }
      shoppingCart.setNumber(1);
      shoppingCart.setCreateTime(LocalDateTime.now());
 
      // 插入数据到数据库
      shoppingCartMapper.insert(shoppingCart);
    }
    
  }
  
  /**
   * 查询所有购物车项目的列表。
   */
  @Override
  public List<ShoppingCart> list() {
    ShoppingCart shoppingCart = new ShoppingCart();
    shoppingCart.setUserId(BaseContext.getCurrentId());
    List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
    return list;
  }
  
  /**
   * 从购物车中移除指定的商品。
   * @param shoppingCartDTO 包含要移除商品信息的数据传输对象，可以是菜品或套餐的ID及相关属性。
   */
  @Override
  public void sub(ShoppingCartDTO shoppingCartDTO) {
    ShoppingCart shoppingCart = new ShoppingCart();
    BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
    
    List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
    shoppingCart = list.get(0);
    
    if(shoppingCart.getNumber() > 1) {
      // 更新数量
      shoppingCart.setNumber(shoppingCart.getNumber() - 1);
      shoppingCartMapper.update(shoppingCart);
    }else {
      // 移除
      shoppingCartMapper.delete(shoppingCart.getId());
    }
    
  }
  
  /**
   * 清空当前用户的购物车。
   * 该方法会删除与当前登录用户关联的所有购物车条目，从而清空用户的购物车。
   */
  @Override
  public void clean() {
    Long userId = BaseContext.getCurrentId();
    shoppingCartMapper.deleteByUserId(userId);
  }
}